
package com.ifcar.app.activity;

import com.ifcar.app.activity.mycomment.MyCommentTabFragment;
import com.ifcar.app.fragment.FragmentUtil;
import com.ifcar.app.fragment.NRTabFragment;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.activity.R;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

public class MyCommentsActivity extends BaseActivity implements OnClickListener,
        OnSharedPreferenceChangeListener {

    private SharedPreferences mSPref;

    private String mAccount;
    
    private MyCommentTabFragment mMyCommentTabFragment;
    
    private boolean mPaused = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        mSPref = PreferenceManager.getDefaultSharedPreferences(this);
        mSPref.registerOnSharedPreferenceChangeListener(this);
        setContentView(R.layout.mycomment_layout);
        
        hideIcon();
        View titleLayout = findViewById(R.id.top_main_bottom);
        TextView title = (TextView) titleLayout.findViewById(R.id.top_main_text);
        title.setText(R.string.comment_myself);
        titleLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mMyCommentTabFragment != null) {
                    mMyCommentTabFragment.dispatchActivityViewClick(v);
                }
            }

        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (isLoginChanged()) {
            onLoginChanged();
        }
        mPaused = false;
    }
    
    @Override
    protected void onPause() {
        mPaused = true;
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSPref.unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (mMyCommentTabFragment != null && mMyCommentTabFragment.dispatchKeyEvent(event)) {
            return true;
        }
        return super.dispatchKeyEvent(event);
    }
    
    private boolean isLogin() {
        return !TextUtils.isEmpty(mAccount);
    }

    private boolean isLoginChanged() {
        return !getAccount().equals(mAccount);
    }

    public String getAccount() {
        return mSPref.getString("account", "");
    }

    public void logout() {
        if (isLogin()) {
            mSPref.edit().putString("account", "").commit();
            onLoginChanged();
        }
    }

    private void onLoginChanged() {
        mAccount = getAccount();
        if (isLogin()) {
            mMyCommentTabFragment = null;
            FragmentManager fm = getSupportFragmentManager();
            Fragment f = fm.findFragmentByTag(MyCommentTabFragment.TAG);
            if (f != null) {
                ((NRTabFragment)f).removeAllTabFragment();
                FragmentTransaction ft = fm.beginTransaction();
                ft.remove(f);
                FragmentUtil.commitAndExec(fm, ft, false);
            }
            Bundle args = new Bundle();
            args.putString("account", mAccount);
            mMyCommentTabFragment = (MyCommentTabFragment)addFragmentByTag(R.id.mycomment_tab_container,
                    MyCommentTabFragment.class, MyCommentTabFragment.TAG, args);
            mMyCommentTabFragment.setup();
            mMyCommentTabFragment.selectInTab();
            
            setViewVisibility(R.id.mycomment_tab_container, View.VISIBLE);
            setViewVisibility(R.id.logout_content, View.GONE);
        } else {
            mMyCommentTabFragment = null;
            FragmentManager fm = getSupportFragmentManager();
            Fragment f = fm.findFragmentByTag(MyCommentTabFragment.TAG);
            if (f != null) {
                ((NRTabFragment)f).removeAllTabFragment();
                
                FragmentTransaction ft = fm.beginTransaction();
                ft.remove(f);
                FragmentUtil.commitAndExec(fm, ft, false);
            }
            f = fm.findFragmentByTag(MyCommentTabFragment.TAG);
            setViewVisibility(R.id.mycomment_tab_container, View.GONE);
            setViewVisibility(R.id.logout_content, View.VISIBLE);
        }
    }

    public void setViewVisibility(int viewid, int visible) {
        View view = findViewById(viewid);
        if (view != null) {
            view.setVisibility(visible);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.login:
                Intent intent = new Intent(MyCommentsActivity.this, AccountLogin.class);
                intent.putExtra("isonlylogin", true);
                startActivity(intent);
                break;
            default:
                break;
        }

    }

    public void goBackClick(View view) {
        finish();
    }

    @Override
    public void onApplyTheme() {
        super.onApplyTheme();
        ThemeSettingsHelper themeSettingsHelper = getThemeSettingsHelper();
        themeSettingsHelper.setTextViewColor(this, R.id.top_main_text, R.color.main_title_color);
        themeSettingsHelper.setImageViewSrc(this, R.id.top_id_image, R.drawable.netease_top);
        themeSettingsHelper.setViewBackgroud(this, R.id.top_main_bottom, R.drawable.top_background);
        themeSettingsHelper.setViewBackgroudColor(this, R.id.main, R.color.main_bg_color);
        themeSettingsHelper.setViewBackgroud(this, R.id.login, R.drawable.netease_big_button);
        themeSettingsHelper.setTextViewColor(this, R.id.login, R.color.list_title_color);
        themeSettingsHelper.setTextViewColor(this, R.id.login_msg, R.color.list_title_color);

        // 返回夜间模式
        themeSettingsHelper.setImageViewSrc(this, R.id.go_back_common_btn, R.drawable.go_back);
        
        if (mMyCommentTabFragment != null) {
            mMyCommentTabFragment.applyTheme();
        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if ("account".equals(key)) {
            mHandler.post(new Runnable() {
                public void run() {
                    if (!mPaused && isLoginChanged()) {
                        onLoginChanged();
                    }
                }
            });
        }
    }

    Handler mHandler = new Handler() {

    };
}
